import cherrypy
import ldap


class Auth:
    def __init__(self, session):
        self.sess = session

        self.session_key = 'GIUopkp98987YHUIOKLI*^&*'
        self.admin = '$%^&*(0-%^&890-*^&*'
        self.users = {
            'nikolay.yakovlev@gameloft.com': '',
            'konstantin.teslenko@gameloft.com': 'admin',
            'alexandr.soor@gameloft.com': 'admin',
            'boris.ivakha@gameloft.com': '',
            'boris.kozar@gameloft.com': '',
            'denis.belyaev@gameloft.com': 'admin',
            'roman.yatsenko@gameloft.com': '',
            'roman.drovikov@gameloft.com': '',
            'vitaliy.shpotov@gameloft.com': '',
            'stanislav.podgorskiy@gameloft.com': '',
            'vadim.kravchenko@gameloft.com': '',
            'alexey.nemets@gameloft.com': '',
            'yuriy.boltyan@gameloft.com': '',
            'bogdan.klochko@gameloft.com': '',
            'dora.magyary@gameloft.com': '',
            'georgebogdan.crisan@gameloft.com': '',
            'cosminionut.ilie@gameloft.com': '',
            'andreialexandru.ilie@gameloft.com': '',
            'tiberiu.varaticeanu@gameloft.com': '',
            'alexandrupetre.floristeanu@gameloft.com': '',
            'mohamedazzamdawud.afaneh@gameloft.com': '',
            'raulioan.chirai@gameloft.com': '',
            'renata.szakacs@gameloft.com': '',
            'sergiu.soptirean@gameloft.com': '',
            'bogdanandrei.broju@gameloft.com': '',
            'yevgeniy.olexandr@gameloft.com': 'admin'
        }

    def logout(self):
        self.sess[self.session_key] = False

    def auth(self, username, password):
        username = "%s" % username.lower()

        if self.ldap(username, password):
            self.sess[self.session_key] = True
            self.sess['username'] = username

            if username in self.users and self.users[username] == 'admin':
                self.sess[self.session_key + '_admin'] = True
            else:
                self.sess[self.session_key + '_user'] = True

            return True

        self.sess[self.session_key] = False
        return False

    def ldap(self, username=None, password=None):
        con = ldap.initialize('ldap://kha-dc01.gameloft.org')
        try:
            data = con.simple_bind_s(username, password)
            return True
        except:
            return False

    def get_name(self):
        return self.sess['username']

    def is_admin(self):
        try:
            return self.sess[self.session_key + '_admin']
        except KeyError:
            return False

    def is_moder(self):
        try:
            return self.sess[self.session_key + '_moder']
        except KeyError:
            return False

    def is_user(self):
        try:
            return self.sess[self.session_key + '_user']
        except KeyError:
            return False


    def is_auth(self):
        try:
            return self.sess[self.session_key]
        except KeyError:
            self.sess[self.session_key] = False
            return False
